package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

import ConfigFile.ConfigFile;
import ConfigFile.Loads;

import sound.ILoadMusic;
import sound.LoadMusic;
import sound.Sounds;

import model.general.LoadImage;
import model.general.Location;
import model.general.Util;
import model.objects.FunnyObject;
import model.objects.IAnimateMainActor;
import model.objects.Lighting;
import model.observer.Obsever;

import control.ControlGame;
import control.IControlGame;
import control.KeyEventGame;

public class Map extends JPanel implements Obsever {
	private IControlGame control;

	private int PWIDTH = Util.DEFAULT_WIDTH_PANEL,
			PHEIGHT = Util.DEFAULT_HEIGHT_PANEL;

	private FunnyObject bubble;
	private BufferedImage gameImagePause;
	private BufferedImage gameImageStatusbar;
	private BufferedImage heart;
	private BufferedImage gameImageStart;
	private BufferedImage gameImageUplevel;
	private BufferedImage gameImageWin;
	private BufferedImage palm;

	private IAnimateMainActor boat;

	private LoadImage loadImg = new LoadImage();

	private int lifePlayer = Util.LIFE;
	private int level = 1;
	private int score = 0;

	private boolean isStart = false;

	// for displaying messages
	private Font msgsFont;
	private FontMetrics metrics;

	private ILoadMusic music_plus_score = new LoadMusic(Sounds.CATCH_GOOD_ITEM,
			false);

	public Map() {
		setDoubleBuffered(false);
		setPreferredSize(new Dimension(PWIDTH, PHEIGHT));
		setBackground(Color.black);
		setFocusable(true);
		requestFocus(); // the JPanel now has focus, so receives key events

		control = new ControlGame(this);
		boat = control.getBoat();

		// set up message font
		msgsFont = new Font("SansSerif", Font.BOLD, 24);
		metrics = this.getFontMetrics(msgsFont);

		// ve bong bong'
		String[] arrImgBubbles = { "bubbles.gif", "bubbles-2.gif",
				"bubbles-3.gif" };
		bubble = new FunnyObject(new Location(700, 400), arrImgBubbles[0],
				arrImgBubbles[0], this, arrImgBubbles);
		bubble.setImgChange(arrImgBubbles);

		gameImagePause = loadImg.getbufferImage("game_paused.png");
		gameImageStatusbar = loadImg.getbufferImage("status_bar.png");
		gameImageStart = loadImg.getbufferImage("start.jpg");
		gameImageUplevel = loadImg.getbufferImage("level_up.png");
		gameImageWin = loadImg.getbufferImage("game_paused.png");
		heart = loadImg.getbufferImage("heart-icon.png");
		palm = loadImg.getbufferImage("palm-tree-icon.png");

		KeyEventGame keyEvent = new KeyEventGame(control);
		addKeyListener(keyEvent);

	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		paintGameStartWarning(g);
	}

	@Override
	public void paintComponents(Graphics arg0) {
		super.paintComponents(arg0);
		control.gameRender();
	}

	public void updateAllElement() {
		bubble.update();
	}

	public void paintSomethingFun(Graphics g) {
		bubble.show(g);
		g.drawImage(palm, 600, 70, null);
	}

	public void paintGameStartWarning(Graphics g) {
		g.drawImage(gameImageStart, 0, 0, null);
	}

	/* paint the game pause warning */
	public void paintGamePausedWarning(Graphics g) {
		paintStatusBar(g);
		paintSomethingFun(g);
		g.drawImage(gameImagePause, Util.DEFAULT_WIDTH_PANEL / 6,
				Util.DEFAULT_HEIGHT_PANEL / 3, null);
	}

	public void paintGameWonWarning(Graphics g) {
		paintStatusBar(g);
		paintSomethingFun(g);
		g.drawImage(gameImageWin, Util.DEFAULT_WIDTH_PANEL / 6,
				Util.DEFAULT_HEIGHT_PANEL / 3, null);
	}

	public void paintGameLostWarning(Graphics g) {
		paintStatusBar(g);
		paintSomethingFun(g);
	}

	/* warning before playing a normal level */
	public void paintGameLevelUp(Graphics g) {
		paintStatusBar(g);
		paintSomethingFun(g);
		g.drawImage(gameImageUplevel, Util.DEFAULT_WIDTH_PANEL / 6,
				Util.DEFAULT_HEIGHT_PANEL / 3, null);
	}

	public void paintStatusBar(Graphics g) {
		int widthStatusBar = gameImageStatusbar.getWidth();
		int heightStatusBar = gameImageStatusbar.getHeight();

		int xDraw = this.getWidth() - widthStatusBar;
		int yDraw = 0;

		// paint status bar
		g.fillRect(0, 0, this.getWidth(), heightStatusBar);
		g.drawImage(gameImageStatusbar, xDraw, yDraw, null);

		// paint option of boat
		paintOptionOfBoat(g);

	}

	public void paintGameElements(Graphics g) {

	}

	public void paintOptionOfBoat(Graphics g) {
		int xHeart = this.getX();

		for (int i = 0; i < lifePlayer; i++) {
			g.drawImage(heart, xHeart, 10, null);
			xHeart += 15;
		}

		g.setFont(new Font("Courier New", Font.PLAIN, 12));
		/* bottom status bar */
		g.setColor(Color.white);
		g.drawString("LEVEL: " + level, 139, 20);
		g.setColor(Color.YELLOW);
		g.drawString("SCORE: " + score, 209, 20);
		g.setColor(Color.black);
	}

	/**
	 * update option of boat when the boat is be change
	 */
	@Override
	public void check() {
		lifePlayer = boat.getLife();
		score = boat.getScore();
		// System.out.println("viem is checking: life=" + lifePlayer +
		// " / score="
		// + score);
		paintOptionOfBoat(getGraphics());
		music_plus_score.playMusic();
		control.checkLevel(score, lifePlayer, level);
	}

	public void levelUp() {
		this.level++;
	}

	public void setLife(int life) {
		this.lifePlayer = life;
	}
	
	public void setLevel(int level){
		this.level = level;
	}
	public void setScore(int score){
		this.score = score;
	}
	public void setIsStartGame(boolean isbeforeStart) {
		this.isStart = isbeforeStart;
	}
	
	public boolean getIsStart(){
		return isStart;
	}

	public void pause() {
		control.pauseGame();
		
	}

}
